/*
 * MockSelectableChannel.java    0.0.1    21/set/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.net.cs.server.impl.mock;

import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.SelectorProvider;

/**
 * Abstract base class for 
 * {@link org.mentalsmash.tazio.net.cs.server.impl.mock.MockWritableSelectableChannel MockWritableSelectableChannel} 
 * and 
 * {@link org.mentalsmash.tazio.net.cs.server.impl.mock.MockReadableSelectableChannel MockReadableSelectableChannel}
 * <p>
 * Both concrete classes are used for testing purposes.
 * </p>
 * @version    0.0.1    21/set/2009    
 * @author     Andrea Reale <andrea.reale@gmail.com>
 */
public abstract class MockSelectableChannel extends SelectableChannel {
	
    public final static int CHANNEL_SIZE = 1024;

	private final Object lock = new Object();
	
	private volatile boolean blocking =  true;
	
	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#blockingLock()
	 */
	@Override
	public Object blockingLock() {
		return lock;
	}
	
	/**
	 * The end of the pipe that is being exposed to the public
	 * @return The end of the pipe that is being exposed to the public
	 */
	protected abstract SelectableChannel getChannel();
	
	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#configureBlocking(boolean)
	 */
	@Override
	public SelectableChannel configureBlocking(boolean block)
			throws IOException {
		blocking = block;
		getChannel().configureBlocking(blocking);
		
		return this;
	}

	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#isBlocking()
	 */
	@Override
	public boolean isBlocking() {
		return blocking;
	}

	
	
	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#isRegistered()
	 */
	@Override
	public boolean isRegistered() {
		return getChannel().isRegistered();
	}

	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#keyFor(java.nio.channels.Selector)
	 */
	@Override
	public SelectionKey keyFor(Selector sel) {
		return getChannel().keyFor(sel);
	}

	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#provider()
	 */
	@Override
	public SelectorProvider provider() {
		return getChannel().provider();
	}

	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#register(java.nio.channels.Selector, int, java.lang.Object)
	 */
	@Override
	public SelectionKey register(Selector sel, int ops, Object att)
			throws ClosedChannelException {
		
		
		return getChannel().register(sel, ops, att);
	}

	/* (non-Javadoc)
	 * @see java.nio.channels.SelectableChannel#validOps()
	 */
	@Override
	public int validOps() {
		return getChannel().validOps();
	}



}
